package com.ewanghuiju.app.ui.shopmail.activity;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.ewanghuiju.app.R;
import com.ewanghuiju.app.app.Constants;
import com.ewanghuiju.app.base.BaseActivity;
import com.ewanghuiju.app.base.contract.shopmail.TodayVogueContract;
import com.ewanghuiju.app.model.bean.response.MailShopResponBean;
import com.ewanghuiju.app.presenter.shopmail.TodayVoguePresenter;
import com.ewanghuiju.app.ui.shopmail.adapter.MailShopAdapter;
import com.ewanghuiju.app.util.LoadingDialogUtils;
import com.ewanghuiju.app.util.nodoubleclick.AntiShake;
import com.ewanghuiju.app.widget.FixImageview;
import com.google.android.material.appbar.AppBarLayout;
import com.gyf.immersionbar.ImmersionBar;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author sj  qq:751943509
 * @class 今日爆款   高佣专区  大额神券
 * @time 2020/2/27
 */
public class TodayVogueActivity extends BaseActivity<TodayVoguePresenter> implements TodayVogueContract.View {
    @BindView(R.id.appBarLayout)
    AppBarLayout appBarLayout;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.coordinatorLayout)
    CoordinatorLayout coordinatorLayout;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.iv_head_center)
    ImageView ivHeadCenter;
    @BindView(R.id.fiv_top)
    FixImageview fivTop;
    @BindView(R.id.tv_screen_jg)
    TextView tvScreenJg;
    @BindView(R.id.tv_screen_yj)
    TextView tvScreenYj;
    @BindView(R.id.tv_screen_xl)
    TextView tvScreenXl;
    @BindView(R.id.tv_screen_zh)
    TextView tvScreenZh;
    @BindView(R.id.iv_screen_jg)
    ImageView ivScreenJg;
    @BindView(R.id.iv_screen_yj)
    ImageView ivScreenYj;
    @BindView(R.id.iv_screen_xl)
    ImageView ivScreenXl;

    private MailShopAdapter mailShopAdapter;

    private int mType;

    private int currentPage = 1;

    private int sort = 2;

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_today_vogue;
    }

    @Override
    protected void initEventAndData() {
        mType = getIntent().getIntExtra(Constants.MAIL_TYPE, Constants.MAIL_TYPE_TODAY_HOT);
        mToolbar.setBackground(null);
//        mToolbar.setNavigationIcon(R.mipmap.icon_fan_white);
        tvTitle.setVisibility(View.GONE);
        ivHeadCenter.setVisibility(View.VISIBLE);
        if (mType == Constants.MAIL_TYPE_TODAY_HOT) {
            ivHeadCenter.setImageResource(R.mipmap.icon_jrbk_head);
            Glide.with(mContext).load(R.mipmap.icon_jrbk_banner).into(fivTop);
        } else if (mType == Constants.MAIL_TYPE_GYZQ) {
            ivHeadCenter.setImageResource(R.mipmap.icon_gyzq_head);
            Glide.with(mContext).load(R.mipmap.icon_gyzq_banner).into(fivTop);
        } else if (mType == Constants.MAIL_TYPE_DESQ) {
            ivHeadCenter.setImageResource(R.mipmap.icon_desq_head);
            Glide.with(mContext).load(R.mipmap.icon_desq_banner).into(fivTop);
        }
//        initBanner();
    }

    @Override
    protected void intSmartRefreshLayout() {
        refreshLayout.setEnableRefresh(true);//是否启用下拉刷新功能
        refreshLayout.setEnableLoadMore(true);//是否启用上拉加载功能
        refreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                LoadingDialogUtils.show(mContext);
                currentPage = currentPage + 1;
                mPresenter.getMoreData(mType, currentPage);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                LoadingDialogUtils.show(mContext);
                currentPage = 1;
                mPresenter.getdata(mType, currentPage);
            }
        });
//        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
//            @Override
//            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
//                LoadingDialogUtils.show(mContext);
//                currentPage = 1;
//                mPresenter.getdata(mType, currentPage);
//            }
//        });
        intRecyleview();
    }


    private void intRecyleview() {
        recyclerView.setNestedScrollingEnabled(false);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        mailShopAdapter = new MailShopAdapter(R.layout.item_mail_shop);
//        mailShopAdapter.setNewData(TestBean.getMailShopResponBeanBeanList(10));
        mailShopAdapter.setHasStableIds(true);
        //加载更多 参考 https://github.com/CymChad/BaseRecyclerViewAdapterHelper/wiki/Pull-up-to-loadMore
//        mailShopAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
//            @Override
//            public void onLoadMoreRequested() {
//                LoadingDialogUtils.show(mContext);
//                mPresenter.getMoreData(mType,currentPage);
//            }
//        }, recyclerView);
        recyclerView.setAdapter(mailShopAdapter);
//        mailShopAdapter.disableLoadMoreIfNotFullPage();
        refreshLayout.autoRefresh();
    }


    @OnClick({R.id.layout_zh, R.id.layout_jg, R.id.layout_yj, R.id.layout_xl})
    public void doClick(View view) {
        if (AntiShake.check(view.getId())) {    //判断是否多次点击
            return;
        }
        switch (view.getId()) {
            case R.id.layout_zh:
                reSetScreen();
                tvScreenZh.setTextColor(ContextCompat.getColor(mContext, R.color.colorPrimary));
                sort = 2;
                break;
            case R.id.layout_jg:
                reSetScreen();
                tvScreenJg.setTextColor(ContextCompat.getColor(mContext, R.color.colorPrimary));
                if (sort == 10) {
                    sort = 9;
                    ivScreenJg.setImageResource(R.mipmap.icon_screen_up);
                } else {
                    sort = 10;
                    ivScreenJg.setImageResource(R.mipmap.icon_screen_down);
                }
                break;
            case R.id.layout_yj:
                reSetScreen();
                tvScreenYj.setTextColor(ContextCompat.getColor(mContext, R.color.colorPrimary));
                if (sort == 4) {
                    sort = 3;
                    ivScreenYj.setImageResource(R.mipmap.icon_screen_up);
                } else {
                    sort = 4;
                    ivScreenYj.setImageResource(R.mipmap.icon_screen_down);
                }
                break;
            case R.id.layout_xl:
                reSetScreen();
                tvScreenXl.setTextColor(ContextCompat.getColor(mContext, R.color.colorPrimary));
                if (sort == 2) {
                    sort = 1;
                    ivScreenXl.setImageResource(R.mipmap.icon_screen_up);
                } else {
                    sort = 2;
                    ivScreenXl.setImageResource(R.mipmap.icon_screen_down);
                }
                break;
            default:
                break;
        }
    }

    /**
     * 重置顶部筛选
     */
    public void reSetScreen() {
        tvScreenZh.setTextColor(ContextCompat.getColor(mContext, R.color.color_666));
        tvScreenJg.setTextColor(ContextCompat.getColor(mContext, R.color.color_666));
        tvScreenYj.setTextColor(ContextCompat.getColor(mContext, R.color.color_666));
        tvScreenXl.setTextColor(ContextCompat.getColor(mContext, R.color.color_666));
        ivScreenJg.setImageResource(R.mipmap.icon_screen_normal);
        ivScreenYj.setImageResource(R.mipmap.icon_screen_normal);
        ivScreenXl.setImageResource(R.mipmap.icon_screen_normal);
    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        ImmersionBar.with(this)
                .titleBar(mToolbar, true)
                .statusBarDarkFont(true)
                .init();
    }

    @Override
    public void showContent(List<MailShopResponBean> list) {
        LoadingDialogUtils.dismissDialog_ios();
        if (list == null) {
            list = new ArrayList<>();
        }
        int size = list.size();

        mailShopAdapter.setNewData(list);

        if (mailShopAdapter.getData().size() == 0) {    //空视图情况
//            mailShopAdapter.setEmptyView();
        } else if (size< Constants.NUM_OF_PAGE) {
            //上拉加载出来的数据为0，认为加载结束
            refreshLayout.finishRefreshWithNoMoreData();
        } else {//完成了本次加载，还有更多数据（注意逻辑是在else中，这里写错可能导致不停的上拉加载数据）
            refreshLayout.finishRefresh(0);
        }
    }

    @Override
    public void showMoreContent(List<MailShopResponBean> list) {
        LoadingDialogUtils.dismissDialog_ios();
        if (list == null) {
            list = new ArrayList<>();
        }
        mailShopAdapter.addData(list);
//        if (mailShopAdapter.getData().size() == 0) {    //空视图情况
//            mailShopAdapter.setEmptyView();
//        } else if (list.size() < Constants.NUM_OF_PAGE) {                  //上拉加载出来的数据为0，认为加载结束
//            mailShopAdapter.loadMoreEnd(false);
//        } else {//完成了本次加载，还有更多数据（注意逻辑是在else中，这里写错可能导致不停的上拉加载数据）
//            mailShopAdapter.loadMoreComplete();
//        }
        if (mailShopAdapter.getData().size() == 0) {    //空视图情况
//            mailShopAdapter.setEmptyView();
        } else if (list.size() < Constants.NUM_OF_PAGE) {                  //上拉加载出来的数据为0，认为加载结束
            refreshLayout.finishLoadMoreWithNoMoreData();
        }else{
            refreshLayout.finishLoadMore();
        }
    }

    @Override
    public void showContentError() {
        LoadingDialogUtils.dismissDialog_ios();
        refreshLayout.finishRefresh();
    }

    @Override
    public void showMoreContentError() {
        LoadingDialogUtils.dismissDialog_ios();
        mailShopAdapter.loadMoreComplete();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        LoadingDialogUtils.dismissDialog_ios();
    }
}
